package com.wang.server.web;


import com.wang.server.constant.Constants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author 王满平
 * @date 2021/8/21 16:33
 */
@Data
@ApiModel(value = "BaseResponse", description = "返回对象")
public class BaseResponse<T> {

    @ApiModelProperty(value = "返回状态码，0标识success，其他标识error", name = "code")
    private String code;

    @ApiModelProperty(value = "描述信息", name = "message")
    private String message;

    @ApiModelProperty(value = "返回数据体", name = "body")
    private T body;

    public BaseResponse() {
    }

    public BaseResponse(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public BaseResponse(String code, String message, T obj) {
        this.code = code;
        this.message = message;
        this.body = obj;
    }

    /**
     * 操作成功
     *
     * @param message 返回信息
     * @return BaseResponse
     */
    public static <T> BaseResponse<T> success(String message) {
        return new BaseResponse<T>(Constants.RESPONSE_SUCCESS_CODE, message);
    }


    public static <T> BaseResponse<T> success() {
        return new BaseResponse<T>(Constants.RESPONSE_SUCCESS_CODE, "响应成功");
    }

    /**
     * 操作成功，并返回数据
     *
     * @param message 返回信息
     * @param body    返回数据
     * @return BaseResponse
     */
    public static <T> BaseResponse<T> success(String message, T body) {
        return new BaseResponse<T>(Constants.RESPONSE_SUCCESS_CODE, message, body);
    }

    /**
     * 操作失败，返回错误码和信息
     *
     * @param code    错误码
     * @param message 错误信息
     * @return BaseResponse
     */
    public static <T> BaseResponse<T> error(String code, String message) {
        return new BaseResponse<T>(code, message);
    }

    /**
     * 操作失败，返回错误码和信息
     *
     * @param code    错误码
     * @param message 错误信息
     * @param body    错误数据
     * @return BaseResponse
     */
    public static <T> BaseResponse<T> error(String code, String message, T body) {
        return new BaseResponse<T>(code, message, body);
    }

}
